package com.novel.authorization.staff.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.novel.authorization.staff.entity.NfEmail;
import com.novel.authorization.staff.service.NfEmailService;
import com.novel.base.entity.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 发邮件设置
 *
 * @author 樊温军
 * @date Wed May 07 11:01:07 CST 2025
 */
@Slf4j
@Api(tags = "发邮件设置", value = "发邮件设置")
@RestController
@AllArgsConstructor
@RequestMapping("/nfEmail")
public class NfEmailController {

    private final NfEmailService nfEmailService;

    /**
     * 发邮件设置—列表
     *
     * @param nfEmail 发邮件设置
     * @return R<List < NfEmail>>
     */
    @SaCheckPermission("nfEmail:list")
    @ApiOperation(value = "发邮件设置—列表", notes = "发邮件设置—列表")
    @GetMapping("/list")
    public R<List<NfEmail>> getNfEmailList(NfEmail nfEmail) {
        log.info("开始查询发邮件设置—列表，参数：{}", nfEmail);
        try {
            return R.ok(nfEmailService.getNfEmailList(nfEmail), "查询成功");
        } catch (Exception e) {
            log.error("查询发邮件设置—列表时发生错误：{}", e.getMessage(), e);
            return R.failed("查询发邮件设置—列表时发生错误，请稍后重试！");
        }
    }

    /**
     * 发邮件设置—列表—分页
     *
     * @param page    分页对象
     * @param nfEmail 发邮件设置
     * @return R<IPage < NfEmail>>
     */
    @SaCheckPermission("nfEmail:page")
    @ApiOperation(value = "发邮件设置—列表—分页", notes = "发邮件设置—列表—分页")
    @GetMapping(value = "/page")
    public R<IPage<NfEmail>> getNfEmailPage(Page<NfEmail> page, NfEmail nfEmail) {
        log.info("开始查询发邮件设置—列表—分页，第 {} 页，每页 {} 条记录，其他参数：{}", page.getCurrent(), page.getSize(), nfEmail);
        try {
            IPage<NfEmail> iPage = nfEmailService.getPage(page, nfEmail);
            return R.ok(iPage, "查询成功");
        } catch (Exception e) {
            log.error("查询发邮件设置—列表—分页时发生错误：{}", e.getMessage(), e);
            return R.failed("查询发邮件设置—列表—分页时发生错误，请稍后重试！");
        }
    }

    /**
     * 发邮件设置—保存
     *
     * @param nfEmail 发邮件设置
     * @return R<Long>
     */
    @SaCheckPermission("nfEmail:saveOne")
    @ApiOperation(value = "发邮件设置—保存", notes = "发邮件设置—保存")
    @PostMapping(value = "/saveOne")
    public R<Long> saveOneNfEmail(@Valid @RequestBody NfEmail nfEmail) {
        log.info("开始保存发邮件设置：{}", nfEmail);
        try {
            Long id = nfEmailService.saveOneNfEmail(nfEmail);
            log.info("发邮件设置保存成功，ID: {}", id);
            return R.ok(id, "保存成功");
        } catch (Exception e) {
            log.error("保存发邮件设置时发生错误：{}", e.getMessage(), e);
            return R.failed("保存发邮件设置时发生错误，请稍后重试！");
        }
    }

    /**
     * 发邮件设置—删除
     *
     * @param id 发邮件设置主键
     * @return R<Boolean>
     */
    @SaCheckPermission("nfEmail:delById")
    @ApiOperation(value = "发邮件设置—删除", notes = "发邮件设置—删除")
    @PostMapping(value = "/delById")
    public R<Boolean> delByIdNfEmail(Long id) {
        log.info("开始删除发邮件设置：{}", id);
        if (id == null || id <= 0) {
            log.warn("ID为空或无效: {}", id);
            return R.failed("请传入有效的ID！");
        }
        try {
            nfEmailService.delById(id);
            return R.ok(Boolean.TRUE, "删除成功");
        } catch (Exception e) {
            log.error("删除发邮件设置失败: ID={}, Error={}", id, e.getMessage(), e);
            return R.failed("删除失败，请稍后重试！");
        }
    }

    /**
     * 发邮件设置—详情
     *
     * @param id 发邮件设置主键
     * @return R<NfEmail>
     */
    @SaCheckPermission("nfEmail:details")
    @ApiOperation(value = "发邮件设置—详情", notes = "发邮件设置—详情")
    @GetMapping(value = "/details")
    public R<NfEmail> getNfEmail(Long id) {
        log.info("开始查询发邮件设置：{}", id);
        if (id == null || id <= 0) {
            log.warn("ID为空或无效: {}", id);
            return R.failed("请传入有效的ID！");
        }
        try {
            NfEmail nfEmail = nfEmailService.queryById(id);
            if (nfEmail != null) {
                return R.ok(nfEmail, "查询成功");
            } else {
                return R.failed("未找到发邮件设置");
            }
        } catch (Exception e) {
            log.error("查询发邮件设置详情时发生错误：{}", e.getMessage(), e);
            return R.failed("查询发邮件设置详情时发生错误，请稍后重试！");
        }
    }
}
